package com.zhuanyongjin.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.zhuanyongjin.dao.membersDao;
import com.zhuanyongjin.util.Calenda;
import com.zhuanyongjin.util.DatabaseAccess;
import com.zhuanyongjin.util.EasyMapsManager;
import com.zhuanyongjin.util.MD5;

public class membersDaoImpl implements membersDao{

	public List querymembersDao(int Id,int city,int province,int zone,int role_id,int rows,int page){
		// TODO Auto-generated method stub
		String sql = "select m.Id,m.name from members m where m.Id=? AND m.city=? AND m.province=? " +
		 "AND m.zone=? AND m.role_id=? LIMIT ?,?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(Id);
			easyMapsManager.setPreparedParameter(city);
			easyMapsManager.setPreparedParameter(province);
			easyMapsManager.setPreparedParameter(zone);
			easyMapsManager.setPreparedParameter(role_id);
			easyMapsManager.setPreparedParameter((page - 1) * rows);
			easyMapsManager.setPreparedParameter(rows);
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	 public boolean savemembers (int id,String name){
			DatabaseAccess dao=new DatabaseAccess();
			String sql="insert into members(Id,name) values(?,?);";
			dao.setPreparedParameter(id);
			dao.setPreparedParameter(name);
			boolean b=dao.executeInsert(sql)>0?true:false;
			return b;
		}
		
		public List login (String name,String password){
			DatabaseAccess dao=new DatabaseAccess();
			MD5 m=new MD5();
			String pw=m.md5s(password);
			String sql="select m.Id,m.name,m.sex,m.mobel,m.email,m.province,m.city,m.bank_card,m.logintime,m.name1," +
					"m.name2,m.name3 from memberview m where m.mobel=? And m.password=? AND is_useful=1;";
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(name);
				easyMapsManager.setPreparedParameter(pw);
				List list = easyMapsManager.executeQuery(sql);
				if(list.size()>0){
					HashMap adu=(HashMap)list.get(0);
					Calenda C=new Calenda();
					String curdate=C.getNowTime().toString();
					String Id=adu.get("Id").toString();
					adu.put("logintime",curdate);
					String sql2="update members set logintime=? where Id=?";
					dao.setPreparedParameter(curdate);
					dao.setPreparedParameter(Id);
					dao.executeUpdate(sql2);
				}
				databaseAccess.close();
				return list;
			}catch(Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		public boolean Logon(String name,String password,String mobel,String sex){
			DatabaseAccess dao=new DatabaseAccess();
			Calenda C=new Calenda();
			String now=C.getNowTime().toString();
		    MD5 m=new MD5();
			String pw=m.md5s(password);
			String sql="insert into members (name,password,role_id,uptime,mobel," +
					"logintime,is_useful,is_email,is_mobel,wait_delete,province,city,zone,is_recognise,is_card_ok,sex) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			dao.setPreparedParameter(name);
			dao.setPreparedParameter(pw);
			//注册用户的身份为2
			dao.setPreparedParameter(2);
			dao.setPreparedParameter(now);
			dao.setPreparedParameter(mobel);
			dao.setPreparedParameter(now);
			dao.setPreparedParameter(1);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(0);
			dao.setPreparedParameter(sex);
			boolean b=dao.executeInsert(sql)>0?true:false;
			return b;
		}
		//查询是否有相同的手机号，相同则不予以注册
		public List samename(String name){
			DatabaseAccess dao=new DatabaseAccess();
			String sql="select * from members where mobel=?;";
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(name);
				List list = easyMapsManager.executeQuery(sql);
				databaseAccess.close();
				return list;
			}catch(Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		public boolean uplogintime(String id){
			DatabaseAccess dao=new DatabaseAccess();
			System.out.println(id);
			Date now=new Date();
			String sql="update members set logintime=?,logincount=logincount+1 where Id=?;";
			dao.setPreparedParameter(now);
			dao.setPreparedParameter(id);
			boolean b=dao.executeUpdate(sql);
			return b;
		}
		
		//更新用户的数据,同时更新表的的更新时间
		  public boolean updatemembers(String name,String sex,int city,
		   		  int province,String email,String bank_card,int id){
			  DatabaseAccess dao=new DatabaseAccess();
			  Calenda C=new Calenda();
			  String now=C.getNowTime().toString();
				String sql="update members set name=?,sex=?,city=?" +
						",province=?,email=?,bank_card=?,uptime=? where Id=?;";
				System.out.println(name);
				dao.setPreparedParameter(name);
				dao.setPreparedParameter(sex);
				dao.setPreparedParameter(city);
				dao.setPreparedParameter(province);
				dao.setPreparedParameter(email);
				dao.setPreparedParameter(bank_card);
				dao.setPreparedParameter(now);
				dao.setPreparedParameter(id);
				boolean b=dao.executeUpdate(sql);
				System.out.println(b);
				return b;
		  }
		
		 
		 public boolean chongzhi(String id, int yongjin){
			 DatabaseAccess dao=new DatabaseAccess();
				String sql="update members set yongjin=? where Id=?;";
				dao.setPreparedParameter(yongjin);
				dao.setPreparedParameter(id);
				boolean b=dao.executeUpdate(sql);
				return b;
		 }
		 
		 public boolean zhifumembers2(String sid, int syongjin){
			    DatabaseAccess dao=new DatabaseAccess();
				String sql="update members set yongjin=? where Id=?;";
				dao.setPreparedParameter(syongjin);
				dao.setPreparedParameter(sid);
				boolean b=dao.executeUpdate(sql);
				return b;
		 }
			public boolean zhifumembers1(String zid, int zyongjin){
				    DatabaseAccess dao=new DatabaseAccess();
					String sql="update members set yongjin=? where Id=?;";
					dao.setPreparedParameter(zyongjin);
					dao.setPreparedParameter(zid);
					boolean b=dao.executeUpdate(sql);
					return b;
			}
			
			public List getprovince(int province){
				DatabaseAccess dao=new DatabaseAccess();
				String sql="select name from dictionary where Id=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(province);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			public List getcityname(int city){
				DatabaseAccess dao=new DatabaseAccess();
				String sql="select name from dictionary where Id=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(city);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			
			public List getcityId(String cityname){
				String sql="select Id from dictionary where name=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(cityname);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			public List getprovinceId(String province){
				String sql="select Id from dictionary where name=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(province);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		
			public List getzoneId(String province){
				String sql="select Id from dictionary where name=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(province);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			
			public List getmodulId(String modul){
				String sql="select Id from modul_category where name=?;";
				try{
					DatabaseAccess databaseAccess = new DatabaseAccess(false);
					EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
					easyMapsManager.setPreparedParameter(modul);
					List list = easyMapsManager.executeQuery(sql);
					databaseAccess.close();
					return list;
				}catch(Exception e) {
					e.printStackTrace();
				}
				return null;
			}
}
